<%@ tag import="static com.eischet.lookup.Functional.join" %>
<%@ tag import="static com.eischet.lookup.Functional.splitString" %>
<%@ tag import="static com.eischet.lookup.Functional.slice" %>
<%@ tag import="static com.eischet.lookup.StringUtilities.shorten" %>
<%@ attribute name="doc" type="com.eischet.lookup.web.SearchResult" required="true" %>
<%@ attribute name="field" type="java.lang.String" required="true" %>

<%@ attribute name="optional" type="java.lang.Boolean" required="false" description="defaults to false" %>
<%@ attribute name="maxLines" type="java.lang.Integer" required="false" description="defaults to 5" %>
<%@ attribute name="highlight" type="java.lang.Boolean" required="false" description="defaults to true" %>
<%@ attribute name="newline" type="java.lang.Boolean" required="false" description="defaults to false" %>
<%@ attribute name="maxChars" type="java.lang.Integer" required="false" description="defaults to 1000" %>
<%@ attribute name="title" type="java.lang.String" required="false" description="title tag for value span, defaults to field name" %>

<%@ attribute name="caption" type="java.lang.String" required="false" %>
<%@ attribute name="styleClass" type="java.lang.String" required="false" %>

<%
    // set defaults for optional parameters that have sensible defaults:
    optional = optional == null ? Boolean.FALSE : optional;
    maxLines = maxLines == null ? 5 : maxLines;
    maxChars = maxChars == null ? 1000 : maxChars;
    highlight = highlight == null ? Boolean.TRUE : highlight;
    newline = newline == null ? Boolean.FALSE : newline;
    String fieldStyleClass = styleClass == null || styleClass.isEmpty() ? "field" : styleClass;
    String captionStyle = styleClass == null || styleClass.isEmpty() ? "fieldCaption" : styleClass + "Caption";
    title = title == null || title.isEmpty() ? field : title;
    // get the value, either highlighted or plain:
    String value = highlight ? doc.highlight(field) : doc.get(field);
    // if the field is not optional, but highlighting returned nothing, try to get the first maxLines lines from
    // the plain field:
    if (!optional && (value == null || value.isEmpty())) {
        value = shorten(join("<br>", slice(splitString(doc.get(field), "\n"), 5)), maxChars, true);
        if (value == null || value.isEmpty()) {
            value = "--";
        }
    }
    if ((value != null && !value.isEmpty()) || !optional) {
        if (caption != null && !caption.isEmpty()) {
            %><span class="<%=captionStyle%>"><%=caption%>:</span> <%
        }
        %><span class="<%=fieldStyleClass%>" title="<%=title%>"><%= value == null || value.isEmpty() ? "" : value %></span><%
        if (newline) {
            %><br><%
        }
    }
%>

